package com.lxl.todo;

/**
 * @Author: luoxianliang
 * @Email: 1322342586@qq.com
 * @CreateTime: 2025-08-19
 * @Description: 二进制求和
 * @Version: 1.0
 * @OtherInfo:
 * 给你两个二进制字符串 a 和 b ，以二进制字符串的形式返回它们的和。
 */

public class Solution34 {
    public String addBinary(String a, String b) {
        StringBuffer ans = new StringBuffer();

        int n = Math.max(a.length(), b.length()), carry = 0;
        for (int i = 0; i < n; ++i) {
            carry += i < a.length() ? (a.charAt(a.length() - 1 - i) - '0') : 0;
            carry += i < b.length() ? (b.charAt(b.length() - 1 - i) - '0') : 0;
            ans.append((char) (carry % 2 + '0'));
            carry /= 2;
        }

        if (carry > 0) {
            ans.append('1');
        }
        ans.reverse();

        return ans.toString();
    }


    public static void main(String[] args) {
        Solution34 sol = new Solution34();
        String s = sol.addBinary("1011", "1011");
        System.out.println("s = " + s);
    }
}
